﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using CommunitySystem.Views;
using CommunitySystem.Assert;
using EHR.Ctrls.QueryDesign;
using CommunitySystem;
using CommunitySystem.DataQueryService;

namespace EHR.Views
{
    public partial class CustomerDesignView : AsyncCtrl
    {
        private ViewInfo _viewInfo;
        private ViewInfo _tempViewInfo;
        public bool IsAdd { get; set; }
        public bool IsSaved { get; private set; }
        public NavMoveSwitch Nav { get; set; }

        private DesignSourceSelecterView SourceSelecterView
        {
            get { return gridDesignSource.Children[0] as DesignSourceSelecterView; }
        }

        public ViewInfo ViewInfo
        {
            get { return _viewInfo; }
            set
            {
                IsAdd = false;
                ShowViewInfo(value);
            }
        }

        private void ShowViewInfo(ViewInfo vi)
        {
            _viewInfo = vi;
            ViewDesignInfo designInfo = new ViewDesignInfo();
            designInfo.FromXml(XElement.Parse(_viewInfo.XML));
            gridDesign.SetViewInfo(designInfo);

        }

        public CustomerDesignView()
        {
            InitializeComponent();

            IsAdd = true;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!gridDesign.Validate())
            {
                return;
            }

            _tempViewInfo = GetDesignResult();

            Worker.IsBusy = true;
            DataQueryClient client = new DataQueryClient();
            if (IsAdd && (!IsSaved))
            {
                _tempViewInfo.ViewID = Guid.NewGuid();
                client.CreateViewCompleted += client_UpdateViewCompleted;
                client.CreateViewAsync(_tempViewInfo);
            }
            else
            {
                client.UpdateViewCompleted += client_UpdateViewCompleted;
                client.UpdateViewAsync(_tempViewInfo);
            }
        }

        private ViewInfo GetDesignResult()
        {
            ViewDesignInfo vdi = gridDesign.GetViewInfo();
            var newcols = gridDesign.GetNewColumns();
            ViewInfo vi = new ViewInfo();
            vi.ViewID = _viewInfo.ViewID;
            vi.ViewName = vdi.ViewName;
            vi.XML = vdi.ToXml(newcols).ToString();

            return vi;
        }

        void client_UpdateViewCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Worker.IsBusy = false;
            if (e.Error != null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败！"); 
            }
            else
            {
                IsSaved = true;
                _viewInfo = _tempViewInfo;
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功！"); 
            }
        }

        private void btnPreView_Click(object sender, RoutedEventArgs e)
        {
            if (!gridDesign.Validate())
            {
                return;
            }

            DataQueryView view = new DataQueryView();
            view.Query(GetDesignResult());
            Nav.ShowNextPage("预览", view);
        }

        private void AsyncCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsAdd)
            {
                SetDesignSourceUI();
                Worker.IsBusy = true;
                DataQueryClient client = new DataQueryClient();
                client.GetQueryTablesCompleted += client_GetQueryTablesCompleted;
                client.GetQueryTablesAsync();
            }
        }

        void client_GetQueryTablesCompleted(object sender, GetQueryTablesCompletedEventArgs e)
        {
            Worker.IsBusy = false;
            SourceSelecterView.Tables = e.Result;
        }

        private void SetDesignSourceUI()
        {
            DesignSourceSelecterView view = new DesignSourceSelecterView();
            view.NextStep += new EventHandler(view_NextStep);
            gridDesignSource.Children.Add(view);
            gridDesignSource.Visibility = Visibility.Visible;
        }

        void view_NextStep(object sender, EventArgs e)
        {
            ShowViewInfo(SourceSelecterView.SelectedViewInfo);
            sbHideDesignSource.Begin();
        }

        private void sbHideDesignSource_Completed(object sender, EventArgs e)
        {
            gridDesignSource.Visibility = Visibility.Collapsed;
            gridDesignSource.Children.Clear();
        }


    }
}
